<?php
namespace App\Models;
use Illuminate\Support\Facades\Input;
Class Member extends BaseModel
{
    public $timestamps = false;

    protected $table = "member";

    public function getOne($where, $fileds = '*', $order = '')
    {
        if (!$order) {
            $order = ['id' => 'desc'];
        }
        return $this->multiSelect($fileds)->multiWhere($where)->multiOrder($order)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }



    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where, $data)
    {
        return $this->_updata($where, $data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($where)
    {
        return $this->_del($where);
    }

    /**
     * 根据id查询用户信息
     */
    public function getMemberById($id){
        return $this->getOne(array('id' => $id))->toArray();
    }

    /**
     * 我的资料完善
     */
    public function member($request)
    {
        //收集信息
        $memberinfo = $this->getMemberById(session('uid'));
        $data['member_name'] = $request->input('member_name');
        $data['pet_name'] = $request->input('pet_name');
        $data['card'] = $request->input('card');
        $data['member_email'] = $request->input('member_email');
        $where['member_mobile'] = $memberinfo['member_mobile'];
        $where['member_passwd'] = md5($request->input('member_passwd'));
        $pass = $this->getOne($where);
        if (!$pass) {
            $res['member'] = 0;
            $res['state'] = 400;
            $res['msg'] = '密码错误!';
            return $res;
        }
        $result = $this->up($where,$data);
        if ($result) {

            //更新invite表信息
            $up['member_name'] = $data['member_name'];
            $invite  = new Invite();
            $invite->up(array('mid' => session('uid')),$up);

            $res['member'] = 1;
            $res['state'] = 200;
            $res['msg'] = '提交成功!';
            return $res;
        }

    }

    /**
     * 密码修改
     */
    public function edpass($request)
    {
        $txtpassword = md5($request->input('txtpassword'));
        $txtNewpassword = md5($request->input('txtNewpassword'));
        $txtNewpassword2 = md5($request->input('txtNewpassword2'));
        $radType        = $request->input('radType');
        if ($radType == 1) {
            $where['member_passwd'] = $txtpassword;
            $data['member_passwd'] = $txtNewpassword;
        }
        if ($radType == 2) {
            $where['trade_passwd'] = $txtpassword;
            $data['trade_passwd'] = $txtNewpassword;
        }
        if ($txtNewpassword !== $txtNewpassword2) {
            $res['state'] = 1;
            $res['msg'] = '新密码与确认密码不匹配!';
            return $res;
        }
        $all = $this->getOne($where);
        if (!$all) {
            $res['state'] = 400;
            $res['msg'] = '原密码错误!';
            return $res;
        }
        $result = $this->up($where,$data);
        if ($result) {
                $res['state'] = 0;
                $res['msg'] = '修改成功请牢记新密码!';
                return $res;

        }

    }

    function getPageNoList($totalPage, $pageNo, $pageSize)
    {
        $disPageList = 0;//定义最大链接显示数额
        $disPageList = $pageNo + $pageSize - 1;//最大链接显示数额赋值，公式为"当前页数 + 最大链接显示数额 -1"
        //前页面导航页数
        $pageNoList = array();
        //循环显示出当前页面导航页数
        if ($disPageList <= $totalPage) {
            for ($i = $pageNo; $i <= $disPageList; $i++) {
                $pageNoList[] = $i;
            }
        } else {
            if ($totalPage < $pageSize) {
                for ($i = 1; $i <= $totalPage; $i++) {
                    $pageNoList[] = $i;
                }
            } else {
                $start = $totalPage - $pageSize + 1;
                for ($i = $start; $i <= $totalPage; $i++) {
                    $pageNoList[] = $i;
                }
            }
        }
        return $pageNoList;
    }


    /**
     * 获取会员列表,带分页
     */
    public function getMemberList($request){
        $keyword = $request['keyword'] ? $request['keyword'] : '';
        $where = [];
        if ($keyword) {
            if (is_numeric($request['keywords'])) {
                $where['member_mobile'] = $request['keywords'];
            } else {
                $where['pet_name'] = ['like', '%' . $request['keywords'] . '%'];
            }
        }
        $start = date("Y-m-d",strtotime($request['starttime']));
        $end = date("Y-m-d",strtotime($request['endtime']));
        $start1 = $start;
        $end1 = $end;
        if($start>$end){
            $start1 = $end;
            $end1 = $start;
        }
        if($request['starttime']&&$request['endtime']){
            $where['updated_at'] = ['glt',[['>',$start1],['<=',$end1]]];;
        }

        $order['id'] = 'desc';
        $data = $this->getList($where, '*', $order, 10);
        //dd($data);
        if ($data) {
            $data = $data->toArray();
            $data['pageNoList'] = $this->getPageNoList($data['last_page'], request('page', 1), 10);
        }
        return $data;

    }


    /**
     * 推广排行榜列表
     */
    public function getTgOrder(){

        $where = [];
        $order['invite_num'] = 'desc';
        $data = $this->getList($where, '*', $order, 10);    //当前仅列出排行榜前10
        if ($data) {
            $data = $data->toArray();
            $data['pageNoList'] = $this->getPageNoList($data['last_page'], request('page', 1), 10);
        }
        return $data;

    }

}